<%@page import="Tools.AccentRemover"%>
<%@page import="Entity.Category"%>
<%@page import="Entity.Category"%>
<%@page import="java.util.List"%>
<%@page import="DAO.DAO_Category"%>
<div class="col-sm-3 col-md-2 sidebar">
    <h4 class="categories-title"><a href="Dashboard.jsp" style="color: #FFF">Categories</a><span id="add-cat" class="pull-right"><i class="fa fa-plus-square"></i></span></h4>
    <div class="nav nav-sidebar" style="padding-left: 5px;">
        <style>
            .dropdown-menu {
                top: auto;
                min-width: 60px;
                padding: 0;
                margin: 0;
                left:138px;
                border-radius: 0;
            }
            .dropdown-menu>li>a {
                padding: 1px 3px;
            }
            .dropdown-menu>li>a:hover {
                background: #ddd;
            }
        </style>
        <%
            DAO_Category daocatlist = new DAO_Category();
            List<Category> items = daocatlist.categoryList();
            for (Category item : items) {
        %>
            <table style='width: 98%'>
        <tr>
            <td style='width: 80%'><a href="Dashboard.jsp?view=category&id=<%=item.getId()%>"><%=item.getName()%></a></td>
    <td style='width: 20%'>
            <button id="smc-<%=item.getId()%>" data-toggle="dropdown" class="pull-right glyphicon glyphicon-cog" style="background: transparent; border: 0"></button>
            

            <ul class="dropdown-menu" role="menu" aria-labelledby="smc-<%=item.getId()%>">
                <li><a href='addStudent.jsp'>Edit</a></li>
                <li><a href='Dashboard.jsp?view=delete-category&cid=<%=item.getId()%>' onclick="return confirm('Are you sure you want to delete this category?')">Delete</a></li>
            </ul>  </td>
                </tr></table>
            <%
                }
            %>

    </div>
    <script>
        $("[id^=\"smc\"]").each(function(){
            $(this).on("click", function(){
                $('#'+this.id).dropdown();
            });
        });
    </script>
    <form id="add-cat-form" style="display: none" class="form-inline" role="form" method="post">
        <div class="form-group">
            <input name="catname" type="text" class="form-control" placeholder="Enter Category Name">
            <input type="hidden" name="action" value="add_new_category">
        </div>
    </form>
    <%
        String action = request.getParameter("action");
        if (action != null) {
            String catname = request.getParameter("catname");
            if (catname == null || catname.isEmpty()) {
                out.print("Please enter category name!");
            } else if (catname.length() > 80) {
                out.print("Exceeded length!");
            } else {
                catname = new String(catname.getBytes("ISO8859_1"), "UTF-8");

                String slug = AccentRemover.toUrlFriendly(catname);

                Category c = new Category(catname, slug, 0);//parentid = 0 mean this is ancestor
                DAO_Category daocat = new DAO_Category();
                int n = daocat.addCategory(c);
                if (n > 0) {
                    response.sendRedirect("Dashboard.jsp");
                }
                daocat.closeConnection();
            }
        }
    %>
</div>